<?php
/**
 * User: hjd-yao
 * Date: 2019/3/20
 * Time: 17:45
 */

namespace App\Helpers;



use Illuminate\Support\Facades\Cache;

class SelfIcon
{
    const ICON_URL_KEY = 'icon_url';
    const ICONS_KEY = 'icons';

    public static function make()
    {
        return new static();
    }

    public function getRequestIcons()
    {
        $icon_url = Cache::get(self::ICON_URL_KEY,false);

        if(!$icon_url||$icon_url!=config('view.icon.url')){
            Cache::forever(self::ICON_URL_KEY,config('view.icon.url'));
        }

        $icons  = Cache::get(self::ICONS_KEY,false);

        if(!$icons||$icon_url!=config('view.icon.url')){
            $icon_url = config('view.icon.url');
            $client = new \GuzzleHttp\Client();
            $response_contents = $client->get($icon_url)->getBody()->getContents();
            $pattern = '/('.config('view.icon.class').'-)\S*?(:before)/is';
            preg_match_all($pattern,$response_contents,$icons);
            $icons = str_replace(':before','',$icons[0]);
            sort($icons);
            Cache::forever(self::ICONS_KEY,$icons);
            app('files')->put(public_path('assets/iconPicker.json'),json_encode($icons));
        }
        return $icons;
    }
}